//
//  MyWelfareViewController.m
//  TenThousand
//
//  Created by 宁宁 on 16/7/24.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "MyWelfareViewController.h"

#import "RedBagModel.h"//红包Model
#import "RedBagTableViewCell.h"//红包cell

@interface MyWelfareViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MyWelfareViewController
{
    NSMutableArray *_AvailableRedArray;
    NSMutableArray *_AUserRedArray;
    NSMutableArray *_overdueRedArray;
    
    NSString *_isLastPage;
}

 static NSString *const RedBagTableViewCellID = @"RedBagTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _AvailableRedArray = [NSMutableArray array];
    _AUserRedArray = [NSMutableArray array];
    _overdueRedArray = [NSMutableArray array];
    
    //注册
    [self registerTableView];
}
- (void)registerTableView{
    [self.tableView registerNib:[UINib nibWithNibName:@"RedBagTableViewCell" bundle:nil] forCellReuseIdentifier:RedBagTableViewCellID];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString *method;
    //已过期
    if (self.index == 2){
        method = @"HongBao.GetoverdueRed";
    //已使用
    }else if(self.index == 1){
        method = @"HongBao.GetAUserRed";
    //可用
    }else method = @"HongBao.GetAvailableRed";
    
    
    [self getRedBagList:method andWithPage:@"0"];
    
    [self refreshPageWithMethod:method];
    
    
}

- (void)getRedBagList:(NSString *)method andWithPage:(NSString *)page{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *loginId = [defaults objectForKey:@"LoginId"];
    
    NSDictionary *dic = @{
                          @"LoginId" : loginId,
                          @"PageIndex" : page
                          };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:method withParaments:dic withSuccessBlock:^(NSDictionary *object) {
        
        NSLog(@"%@", object);
        if ([page isEqualToString:@"0"]) {
            switch (self.index) {
                case 0:
                    [_AvailableRedArray removeAllObjects];
                    break;
                case 1:
                    [_AUserRedArray removeAllObjects];
                    break;
                case 2:
                    [_overdueRedArray removeAllObjects];
                    break;
                default:
                    break;
            }
        }
        
        _isLastPage = [NSString stringWithFormat:@"%@", object[@"Data"][0][@"PageTotal"]];
        
        for (NSDictionary *dic in object[@"Data"][0][@"ItemList"]) {
            RedBagModel *model = [[RedBagModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            switch (self.index) {
                case 0:
                    [_AvailableRedArray addObject:model];
                    break;
                case 1:
                    [_AUserRedArray addObject:model];
                    break;
                case 2:
                    [_overdueRedArray addObject:model];
                    break;
                default:
                    break;
            }
        }
        [_tableView reloadData];
        
    } withFailureBlock:^(NSError *error) {
        
    } progress:nil];
}


//MJ刷新
-(void)refreshPageWithMethod:(NSString *)method{
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                     {
                                         
                                         [self getRedBagList:method andWithPage:@"0"];
                                         
                                         [self.tableView.mj_header endRefreshing];
                                     }];
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        
        
        if (_isLastPage) {
            [self.tableView.mj_footer endRefreshing];
            return;
        }
        
        [self getRedBagList:method andWithPage:@"0"];
        
        [self.tableView.mj_footer endRefreshing];
    }];
    
    
}


#pragma mark === TableViewDelegate ===
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_index == 0) {
        return _AvailableRedArray.count;
    }else if (_index == 1){
        return _AUserRedArray.count;
    }else return _overdueRedArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RedBagTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RedBagTableViewCellID];
    RedBagModel *model = [[RedBagModel alloc] init];
    switch (self.index) {
        case 0:
            model = _AvailableRedArray[indexPath.row];
            break;
        case 1:
            model = _AUserRedArray[indexPath.row];
            break;
        case 2:
            model = _overdueRedArray[indexPath.row];
            break;
        default:
            break;
    }
    
    cell.Price.text = [NSString stringWithFormat:@"¥%d", [model.Price intValue]];
    cell.Name.text = model.Name;
    cell.BorrowAmount.text = [NSString stringWithFormat:@"满%d元可用", [model.BorrowAmount intValue]];
    cell.EndTime.text = [NSString stringWithFormat:@"有效期至:%@",model.EndTime];
    
    cell.selectionStyle = UITableViewCellStyleDefault;
    return cell;
}



- (UITableView *)tableView{
    if (!_tableView) {
        _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0, Screen_Width, Screen_Height - 64 - 40)style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
